package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.ClassDuty;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.DeviceBaseInfo;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.service.base.DeviceBaseInfoService;

public class DeviceBaseInfoAction extends BaseAction {
	@Resource
	private DeviceBaseInfoService deviceBaseInfoService;
	private DeviceBaseInfo deviceBaseInfo;
	private int departmentID ;
	private int  deviceInfoID;
	private int start ;
	
	private int limit ;
	
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getDeviceInfoID() {
		return deviceInfoID;
	}
	public void setDeviceInfoID(int deviceInfoID) {
		this.deviceInfoID = deviceInfoID;
	}
	public int getDepartmentID() {
		return departmentID;
	}
	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}
	public DeviceBaseInfoService getDeviceBaseInfoService() {
		return deviceBaseInfoService;
	}
	public void setDeviceBaseInfoService(DeviceBaseInfoService deviceBaseInfoService) {
		this.deviceBaseInfoService = deviceBaseInfoService;
	}
	public DeviceBaseInfo getDeviceBaseInfo() {
		return deviceBaseInfo;
	}
	public void setDeviceBaseInfo(DeviceBaseInfo deviceBaseInfo) {
		this.deviceBaseInfo = deviceBaseInfo;
	}
	
	public String load(){ //filter是过滤查询条件的~~~
		try{
			List<DeviceBaseInfo> all=null;
			QueryFilter filter = new QueryFilter() ;
			
			if(departmentID==0){
				System.out.println("PPPPP");
				filter.setPagingBean(new PagingBean(start,limit)) ;
			 all=deviceBaseInfoService.getDao().getAll(filter);
			}else{
				
				filter.setPagingBean(new PagingBean(start,limit)) ;
				filter.addFilter(new FieldBean("department.departmentID" ,departmentID ,Operation.EQUAL));
				all = deviceBaseInfoService.getDao().getAll(filter);
			}
			JsonArray jarray=new JsonArray();
			JsonObject result = new JsonObject() ;
			for(DeviceBaseInfo deviceBaseInfo : all){
				JsonObject jobj=new JsonObject();
				jobj.addProperty("deviceInfoID", String.valueOf(deviceBaseInfo.getDeviceInfoID()));
				jobj.addProperty("deviceName", deviceBaseInfo.getDeviceName());
				jobj.addProperty("place", deviceBaseInfo.getDevBuildPlace());
				jobj.addProperty("department", deviceBaseInfo.getDevBuildCompany());
				jobj.addProperty("useDepartment", deviceBaseInfo.getDevUseDepart());
				jarray.add(jobj);
			}
			result.addProperty("totalProperty",deviceBaseInfoService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString = gson.toJson(result) ;
			System.out.println(jsonString) ;
			return SUCCESS ;
		}catch(Exception e){
			e.printStackTrace();
			return SUCCESS;
		}
	}
	
	public String get()	{
		try{
			System.out.println(deviceInfoID);
			DeviceBaseInfo d = deviceBaseInfoService.loadService(deviceInfoID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("deviceInfoID", String.valueOf(d.getDeviceInfoID())) ;
			jobj.addProperty("deviceCode",d.getDeviceCode() ) ;
			jobj.addProperty("deviceKKSCode", d.getDeviceKKSCode()) ;
			jobj.addProperty("deviceName", d.getDeviceName()) ;
			jobj.addProperty("devBuildPlace", d.getDevBuildPlace()) ;
			jobj.addProperty("devPlaceCode", d.getDevPlaceCode()) ;
			jobj.addProperty("devDrawingNo",d.getDevDrawingNo() ) ;
			jobj.addProperty("devSpecification", d.getDevSpecification()) ;
			jobj.addProperty("deviceModel", d.getDeviceModel()) ;
			jobj.addProperty("devOutDate", TimeFormatUtil.formatDate(d.getDevOutDate())) ;
			jobj.addProperty("devOutCode", d.getDevOutCode()) ;
			jobj.addProperty("devBuildCompany", d.getDevBuildCompany()) ;
			jobj.addProperty("devManuFactory", d.getDevManuFactory()) ;
			jobj.addProperty("devUseDepart", d.getDevUseDepart()) ;
			jobj.addProperty("devRunDate",TimeFormatUtil.formatDate(d.getDevRunDate())) ;
			jobj.addProperty("devUseStatus", d.getDevUseStatus()) ;
			jobj.addProperty("devProvider", d.getDevProvider()) ;
			jobj.addProperty("devAddDate",TimeFormatUtil.formatDate( d.getDevAddDate())) ;
			jobj.addProperty("devContractNo", d.getDevContractNo()) ;
			jobj.addProperty("addRemark", d.getAddRemark()) ;
			jobj.addProperty("deviceRemark", d.getDeviceRemark()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	
	public String update() {
		System.out.println("3esdsd"+deviceBaseInfo.getDeviceInfoID());
		try{
			deviceBaseInfoService.updateService(deviceBaseInfo) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String delete(){
		try{
			System.out.println("+++++"+deviceInfoID);
			deviceBaseInfoService.removeService(deviceInfoID);
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
